<?php
require_once('config.php');
require_once('functions/general.php');
require_once('functions/sql.php');
require_once('sql/sql_incident.php');
require_once('sql/sql_area.php');
require_once('sql/sql_offense.php');
require_once('sql/sql_offender.php');
require_once('alien/Smarty/Smarty.class.php');
require_once('pages/incident.php');

define('ACTION_NEW', 'new');

session_start();

$smarty = new Smarty;
$smarty->template_dir
    = array('templates', 'templates/incident/', 'templates/common/');
$smarty->assign('CONFIG', $CONFIG);
$mysql_link = get_mysql_link();

$incident = new Incident($smarty, $mysql_link);
$template = TPL_INCIDENT_VIEW;

$logged_in = logged_in_status($smarty);

if (isset($_GET['action'])) {
    $action = $_GET['action'];
    if ($action == ACTION_NEW) {
        # Display new incident form.
        $template = $incident->show_new_incident_form();
    }
}


else {
    
    if (isset($_POST) and $_POST) {
        //New incident was posted.
        $is_added = $incident->add_new_incident($_POST, $logged_in);
        
        if (!$is_added) {
            $smarty->display(
                $incident->show_new_incident_form());
            exit;
        }
    }

    $view = issetor(GET, 'view', VIEW_LATEST);
    $smarty->assign('view', $view);

    $page = issetor(GET, 'page', 1);
    $page = (is_numeric($page)) ? $page : 1;
    $offset = ($page - 1) * $CONFIG['incidents_per_page'];

    $filter = array();
    $filter['area']    = issetor(GET, 'area');
    $filter['offense'] = issetor(GET, 'offense');
    $filter['user']    = issetor(GET, 'user');

    $incident->show_incidents($view, $filter, $offset);
}

mysql_close($mysql_link);
$smarty->display($template);
?>
